#include "addressedit.h"
#include <QHBoxLayout>
#include <DApplication>
#include <DGuiApplicationHelper>
#include <QGraphicsOpacityEffect>
#include <QUrl>
#include <QLabel>
#include <iostream>
#include <QAction>
#include <src/settings/settings.h>
using namespace std;
DWIDGET_USE_NAMESPACE
AddressEdit::AddressEdit()
{
    timer=new QTimer;
    btn_star=new DPushButton;
    btn_scheme=new DPushButton;
    btn_scheme->setCursor(Qt::ArrowCursor);
    btn_star->setCursor(Qt::ArrowCursor);
    btn_scheme->setFlat(true);
    btn_star->setFlat(true);
    QHBoxLayout* layout=new QHBoxLayout;
    setLayout(layout);
    QWidget *line=new QWidget;
    QGraphicsOpacityEffect *opacityEffect_1=new QGraphicsOpacityEffect;
    opacityEffect_1->setOpacity(0.2);
    line->setGraphicsEffect(opacityEffect_1);
    line->setFixedSize(2,25);

    layout->setSpacing(3);
    layout->setMargin(3);
    layout->addWidget(btn_scheme);
    layout->addWidget(line);
    layout->addStretch();
    layout->addWidget(btn_star);/*
    btn_star->setAutoFillBackground(true);
    btn_scheme->setAutoFillBackground(true);
    QPalette plt;
    plt.setColor(QPalette::Background,QColor(0,0,0,0));
    btn_star->setPalette(plt);
    btn_scheme->setPalette(plt);*/

    timer->setInterval(100);
    timer->start();
    connect(timer,&QTimer::timeout,[=](){setTextMargins(btn_scheme->width()+3,0,30,0);});

    connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [=](DGuiApplicationHelper::ColorType themeType) {

        QColor main_color;
        main_color=DGuiApplicationHelper::instance()->applicationPalette().highlight().color();
        theme=themeType;
        setStar(isStar);
        AddressEdit::updateSafeState();
        if(themeType==DGuiApplicationHelper::DarkType){
            qDebug()<<"Dark";
            line->setStyleSheet("background-color:#FFFFFF");
        }else {
            qDebug()<<"Light";
            line->setStyleSheet("background-color:#000000");
        }
    });
    connect(btn_star,&DPushButton::clicked,this,&AddressEdit::star_click);
    connect(btn_scheme,&DPushButton::clicked,this,&AddressEdit::scheme_click);
    connect(this,&QLineEdit::returnPressed,this,&AddressEdit::updateSafeState);
//    connect(this,&QLineEdit::textChanged,this,&AddressEdit::updateSafeState);
//

}


void AddressEdit::focusInEvent(QFocusEvent *)
{

    selectAll();
}

void AddressEdit::setStar(bool star)
{
    QFile file(setting_path+"bookmark.json");
    file.open(QFile::ReadWrite);
    bool Stared=false;
    const QByteArray bty = file.readAll();
    json_bookmark=QJsonDocument::fromJson(bty).object();
    QJsonArray array_=json_bookmark.value("bookmarkList").toArray();
    for (int i=0;i<array_.size();i++) {
        if(array_[i].toObject().value("url").toString()==text()){
            Stared=true;
        }

    }
    isStar=star;
    if(!star){
        if(theme==DGuiApplicationHelper::DarkType){
            qDebug()<<"Dark";
            btn_star->setIcon(QIcon(":/btn/icons/pushbutton/star.svg"));
        }else {
            qDebug()<<"Light";
            btn_star->setIcon(QIcon(":/btn/icons/pushbutton/star_dark.svg"));
        }
        if(Stared){

            for (int i=0;i<array_.size();i++) {
                if(array_[i].toObject().value("url").toString()==text()){
                    //删除该项
                    cout<<"Delete";
                    array_.removeAt(i);
                    json_bookmark.insert("bookmarkList",array_);
                    QJsonDocument tmp;
                    tmp.setObject(json_bookmark);
                    file.close();
                    file.open(QFile::WriteOnly);
                    file.write(tmp.toJson());
                }

            }
        }
    }else {
        btn_star->setIcon(QIcon(":/btn/icons/pushbutton/star_ok.svg"));
        if(!Stared)
        {
            QJsonObject newbookmark;
            newbookmark.insert("url",text());
            newbookmark.insert("name",title);
            QJsonArray array=json_bookmark.value("bookmarkList").toArray();
            array.insert(json_bookmark.value("bookmarkList").toArray().size(),newbookmark);
            json_bookmark.insert("bookmarkList",array);
            QJsonDocument doc;
            doc.setObject(json_bookmark);
            file.close();
            file.open(QFile::WriteOnly);
            file.write(doc.toJson());
        }

    }
}

void AddressEdit::star_click()
{
    setSelection(0,0);
    isStar=!isStar;
    setStar(isStar);
}

void AddressEdit::scheme_click()
{
    setSelection(0,0);
}
void AddressEdit::updateSafeState()
{

    QUrl url(text());
    if(url.scheme()=="https"){
        if(theme==DGuiApplicationHelper::DarkType){
            btn_scheme->setIcon(QIcon(":/btn/icons/pushbutton/safe.svg"));
            btn_scheme->setStyleSheet("color:#FFFFFF;broder:none");
        }else {
            btn_scheme->setIcon(QIcon(":/btn/icons/pushbutton/safe_dark.svg"));
            btn_scheme->setStyleSheet("color:#000000;broder:none");
        }
        btn_scheme->setText(tr("Safe"));

    }else if(url.scheme()=="http"){
        if(theme==DGuiApplicationHelper::DarkType){
            btn_scheme->setIcon(QIcon(":/btn/icons/pushbutton/unsafe.svg"));
            btn_scheme->setStyleSheet("color:#FFFFF;broder:none");
        }else {
            btn_scheme->setIcon(QIcon(":/btn/icons/pushbutton/unsafe_dark.svg"));
            btn_scheme->setStyleSheet("color:#000000;broder:none");
        }
        btn_scheme->setText(tr("UnSafe"));
//        setTextMargins(95,0,0,0);
    }else if(url.scheme()=="view-source"){
        if(theme==DGuiApplicationHelper::DarkType){
            btn_scheme->setIcon(QIcon(":/btn/icons/pushbutton/source.svg"));
            btn_scheme->setStyleSheet("color:#FFFFFF;broder:none");
        }else {
            btn_scheme->setIcon(QIcon(":/btn/icons/pushbutton/source_dark.svg"));
            btn_scheme->setStyleSheet("color:#000000;broder:none");
        }
        btn_scheme->setText(tr("Source"));

//        setTextMargins(95,0,0,0);
    }
    setCursorPosition(0);


}
